package yanjunwei.rbac.view;


import yanjunwei.rbac.domain.dto.RoleDto;
import yanjunwei.rbac.domain.enums.RoleDataScope;
import yanjunwei.rbac.domain.vo.Result;
import yanjunwei.rbac.domain.vo.RoleVo;
import yanjunwei.rbac.service.IRoleService;


import yanjunwei.rbac.service.impl.RoleService;
import yanjunwei.rbac.util.DateTimUtils;
import yanjunwei.rbac.util.SingletonFactory;

public class RoleView extends BaseView<RoleVo,IRoleService>{
    public static final String MENU = """
            --------------------
                 用户管理
            --------------------
            |  0...返回上级     |
            |  1...用户列表     |
            |  2...创建用户     |
            |  3...更新用户     |
            |  4...切换状态     |
            --------------------
            请输入选项:""";

 

    private RoleView() {
        service =  SingletonFactory.getInstance(RoleService.class);
    }



    public void show() {
        String menuChoice;
        do {
            System.out.print(MENU);
            menuChoice = MainView.input.nextLine();

            switch (menuChoice) {
                case "0":
                    System.out.println("已返回上一级菜单！");
                    break;
                case "1":
                    list();
                    break;
                case "2":
                    create();
                    break;
                case "3":
                    update();
                    break;
                case "4":
                    delete();
                default:
                    System.out.println("输入非法，请输入正确的菜单编号！");
            }
        } while (!"0".equals(menuChoice));
    }




    public void list() {
        Result result = service.list();
        cache = (RoleVo[]) result.data;
        System.out.println("Id\t角色名称\t角色数据访问范围\t更新时间");
        for (RoleVo role : cache) {
            if (role != null) {
                System.out.printf("%d\t%s\t%s\t%s\n", role.id, role.name, role.dataScope, DateTimUtils.format(role.updateAt));
            }
        }
    }


    public void create() {
        System.out.print("请输入角色名称:");
        String name = MainView.input.nextLine();
        System.out.print("请输入角色数据访问范围: 输入的值应该为[个人,部门,全局]其中之一,");

        RoleDataScope dataScope = null;
        do {
            String str = MainView.input.nextLine();

            try {
                dataScope=Enum.valueOf(RoleDataScope.class,str);
            }catch (Exception e){
                System.out.print("输入的值不合法，输入的值应该为[个人,部门,全局]其中之一,请重新输入,");
            }


        } while (dataScope == null);


        RoleDto dto = new RoleDto(name, dataScope);
        Result result = service.create(dto);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public void update() {

        RoleVo role = getCacheById();
        if (role == null) {
            return;
        }


        System.out.print("请输入角色名称,直接回车表示不修改:");
        String name = MainView.input.nextLine();
        name = "".equals(name) ? role.name : name;


        System.out.print("请输入角色数据访问范围,输入的值应该为[个人,部门,全局]其中之一,直接回车表示不修改:");
        RoleDataScope dataScope = null;
        do {
            String str = MainView.input.nextLine();

            if ("".equals(str)){
                dataScope = role.dataScope;
                break;
            }
            try {
                dataScope=Enum.valueOf(RoleDataScope.class,str);
            }catch (Exception e){
                System.out.print("输入的值不合法，输入的值应该为[个人,部门,全局]其中之一,请重新输入,");
            }

        } while (dataScope == null);


        RoleDto dto = new RoleDto(name, dataScope);
        Result result = service.update(dto, role.id);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    private void delete() {
        RoleVo role = getCacheById();
        if (role == null) {
            return;
        }
        System.out.print("输入'Y确认删除，输入其他取消删除:");
        String confirm = MainView.input.nextLine();
        if (!"Y".equalsIgnoreCase(confirm)) {
            System.out.println("您已取消删除");
            return;
        }
        Result result = service.delete(role.id);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }


}





